package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.dto.CouponAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.CouponUpdateDTO;
import cn.tedu.store.commons.pojo.product.entity.Coupon;
import cn.tedu.store.commons.pojo.product.entity.CouponCategory;
import cn.tedu.store.commons.pojo.product.entity.CouponProduct;
import cn.tedu.store.commons.pojo.product.vo.CouponListItemVO;
import cn.tedu.store.commons.pojo.product.vo.CouponStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.CouponCategoryMapper;
import cn.tedu.store.product.webapi.mapper.CouponMapper;
import cn.tedu.store.product.webapi.mapper.CouponProductMapper;
import cn.tedu.store.product.service.ICouponService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CouponServiceImpl implements ICouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CouponProductMapper couponProductMapper;
    @Autowired
    private CouponCategoryMapper couponCategoryMapper;

    @Override
    public void addNew(CouponAddNewDTO couponAddNewDTO) {
        log.debug("开始处理[添加优惠券]业务 参数:{}",couponAddNewDTO);
        int count = couponMapper.countByName(couponAddNewDTO.getName());
        if(count != 0){
            String message = "添加优惠券失败,名称已被占用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponAddNewDTO,coupon);
        coupon.setCount(couponAddNewDTO.getPublishCount());
        coupon.setUseCount(0);
        coupon.setReceiveCount(0);
        int rows = couponMapper.insert(coupon);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        //插入优惠券和商品分类关系表
        if(coupon.getUseType().equals(1)){
            List<CouponCategory> list = couponAddNewDTO.getCategoryRelationList();
            for (CouponCategory categoryRelation : list) {
                categoryRelation.setCouponId(coupon.getId());
            }
            rows = couponCategoryMapper.insertBatch(list);
            if(rows != list.size()){
                String message = "操作失败,服务器忙,请稍后再试!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_INSERT,message);
            }
        }
        //插入优惠券和商品关系表
        if(coupon.getUseType().equals(2)){
            List<CouponProduct> list = couponAddNewDTO.getProductRelationList();
            for(CouponProduct productRelation : list){
                productRelation.setCouponId(coupon.getId());
            }
            rows = couponProductMapper.insertBatch(list);
            if(rows != list.size()){
                String message = "操作失败,服务器忙,请稍后再试!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_INSERT,message);
            }
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除优惠券]业务 参数:{}", id);
        CouponStandardVO result = couponMapper.getStandardById(id);
        if(result == null){
            String message = "删除优惠券失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = couponMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        //删除商品关系
        rows = couponProductMapper.deleteByCouponId(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

        //删除商品关系
        rows = couponCategoryMapper.deleteByCouponId(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

    }

    @Override
    public void update(Long id, CouponUpdateDTO couponUpdateDTO) {
        CouponStandardVO result = couponMapper.getStandardById(id);
        if(result == null){
            String message = "修改优惠券失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponUpdateDTO,coupon);
        coupon.setId(id);
        int rows = couponMapper.updateById(coupon);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        //删除原有信息 ,插入新信息
        if(coupon.getUseType().equals(1)){
            List<CouponCategory> list = couponUpdateDTO.getCategoryRelationList();
            for (CouponCategory categoryRelation : list) {
                categoryRelation.setCouponId(coupon.getId());
            }
            couponCategoryMapper.deleteByCouponId(id);
            rows = couponCategoryMapper.insertBatch(list);
            if(rows != list.size()){
                String message = "操作失败,服务器忙,请稍后再试!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_INSERT,message);
            }
        }
        //删除原有信息 ,插入新信息
        if(coupon.getUseType().equals(2)){
            List<CouponProduct> list = couponUpdateDTO.getProductRelationList();
            for(CouponProduct productRelation : list){
                productRelation.setCouponId(coupon.getId());
            }
            couponProductMapper.deleteByCouponId(id);
            rows = couponProductMapper.insertBatch(list);
            if(rows != list.size()){
                String message = "操作失败,服务器忙,请稍后再试!";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_INSERT,message);
            }
        }
    }

    @Override
    public List<CouponListItemVO> list() {
        log.debug("开始处理[获取优惠券列表]业务 参数:无");
        return couponMapper.list();
    }

    @Override
    public CouponStandardVO getStandardById(Long id) {
        log.debug("开始处理[获取优惠券详情]业务 参数:{}",id);
        CouponStandardVO result = couponMapper.getStandardById(id);
        if(result == null){
            String message = "获取优惠券详情失败,访问的资源不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }
}
